{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "3ff71f6d",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "g:\\TempTest\\TempDownload\\tk_cwru\\.venv\\Lib\\site-packages\\paddle\\utils\\cpp_extension\\extension_utils.py:711: UserWarning: No ccache found. Please be aware that recompiling all source files may be required. You can download and install ccache from: https://github.com/ccache/ccache/blob/master/doc/INSTALL.md\n",
      "  warnings.warn(warning_message)\n"
     ]
    }
   ],
   "source": [
    "import sys\n",
    "import os\n",
    "import paddle\n",
    "import paddle.nn as nn\n",
    "import paddle.nn.functional as F\n",
    "from paddle.metric import Accuracy\n",
    "import numpy as np\n",
    "import random\n",
    "from scipy.io import loadmat\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn import preprocessing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "1786b3a6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 设置随机种子\n",
    "seed = 102\n",
    "paddle.seed(seed)\n",
    "np.random.seed(seed)\n",
    "random.seed(seed)\n",
    "\n",
    "# 故障标签与数据轴定义\n",
    "FAULT_LABEL_DICT = {\n",
    "    \"97\": 0,\n",
    "    \"105\": 1,\n",
    "    \"118\": 2,\n",
    "    \"130\": 3,\n",
    "    \"169\": 4,\n",
    "    \"185\": 5,\n",
    "    \"197\": 6,\n",
    "    \"209\": 7,\n",
    "    \"222\": 8,\n",
    "    \"234\": 9,\n",
    "}\n",
    "AXIS = \"_DE_time\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "d5ab9cd8",
   "metadata": {},
   "outputs": [],
   "source": [
    "class CWRUDataset(paddle.io.Dataset):\n",
    "    \"\"\"\n",
    "    继承paddle.io.Dataset类\n",
    "    \"\"\"\n",
    "\n",
    "    def __init__(\n",
    "        self,\n",
    "        data_dir,\n",
    "        time_steps=1024,\n",
    "        window=128,\n",
    "        mode=\"train\",\n",
    "        val_rate=0.3,\n",
    "        test_rate=0.5,\n",
    "        noise=False,\n",
    "        snr=None,\n",
    "        network=\"MLP\",\n",
    "    ):\n",
    "        \"\"\"\n",
    "        实现构造函数,定义数据读取方式,划分训练和测试数据集\n",
    "        time_steps: 样本的长度\n",
    "        window：相邻样本之间重合的点数\n",
    "        mode：数据集合\n",
    "        val_rate:\n",
    "        test_rate:\n",
    "        noise：是否添加噪声\n",
    "        snr：添加噪声的分贝数\n",
    "        network：网络类型(决定生成的数据格式)\n",
    "\n",
    "        \"\"\"\n",
    "        super(CWRUDataset, self).__init__()\n",
    "        self.time_steps = time_steps\n",
    "        self.mode = mode\n",
    "        self.noise = noise\n",
    "        self.snr = snr\n",
    "        self.network = network\n",
    "        self.feature_all, self.label_all = self.transform(data_dir)\n",
    "        # 训练集和验证集的划分\n",
    "        train_feature, val_feature, train_label, val_label = train_test_split(\n",
    "            self.feature_all, self.label_all, test_size=val_rate, random_state=seed\n",
    "        )\n",
    "        # 标准化\n",
    "        train_feature, val_feature = self.standardization(train_feature, val_feature)\n",
    "        # 验证集和测试集的划分\n",
    "        val_feature, test_feature, val_label, test_label = train_test_split(\n",
    "            val_feature, val_label, test_size=test_rate, random_state=seed\n",
    "        )\n",
    "        if self.mode == \"train\":\n",
    "            self.feature = train_feature\n",
    "            self.label = train_label\n",
    "        elif self.mode == \"val\":\n",
    "            self.feature = val_feature\n",
    "            self.label = val_label\n",
    "        elif self.mode == \"test\":\n",
    "            self.feature = test_feature\n",
    "            self.label = test_label\n",
    "        else:\n",
    "            raise Exception(\"mode can only be one of ['train', 'val', 'test']\")\n",
    "\n",
    "    def transform(self, data_dir):\n",
    "        \"\"\"\n",
    "        转换函数,获取数据\n",
    "        \"\"\"\n",
    "        feature, label = [], []\n",
    "        for fault_type in FAULT_LABEL_DICT:\n",
    "            lab = FAULT_LABEL_DICT[fault_type]\n",
    "            totalaxis = \"X\" + fault_type + AXIS\n",
    "            if fault_type == \"97\":\n",
    "                totalaxis = \"X0\" + fault_type + AXIS\n",
    "            # 加载并解析mat文件\n",
    "            mat_data = loadmat(data_dir + fault_type + \".mat\")[totalaxis]\n",
    "            # start, end = 0, self.time_steps\n",
    "            # 每隔self.time_steps窗口构建一个样本，指定样本之间重叠的数目\n",
    "            for i in range(0, len(mat_data) - self.time_steps, window):\n",
    "                sub_mat_data = mat_data[i : (i + self.time_steps)].reshape(\n",
    "                    -1,\n",
    "                )\n",
    "                # 是否往数据中添加噪声\n",
    "                if self.noise:\n",
    "                    sub_mat_data = self.awgn(sub_mat_data, snr)\n",
    "                feature.append(sub_mat_data)\n",
    "                label.append(lab)\n",
    "\n",
    "        return np.array(feature, dtype=\"float32\"), np.array(label, dtype=\"int64\")\n",
    "\n",
    "    def __getitem__(self, index):\n",
    "        \"\"\"\n",
    "        实现__getitem__方法，定义指定index时如何获取数据，并返回单条数据\n",
    "        \"\"\"\n",
    "        feature = self.feature[index]\n",
    "        if self.network == \"CNNNet\":\n",
    "            # 增加一列满足cnn的输入格式要求\n",
    "            feature = feature[np.newaxis, :]\n",
    "        elif self.network == \"ResNet\":\n",
    "            # 增加一列并将通道复制三份满足resnet的输入要求\n",
    "            n = int(np.sqrt(len(feature)))\n",
    "            feature = np.reshape(feature, (n, n))\n",
    "            feature = feature[np.newaxis, :]\n",
    "            feature = np.concatenate((feature, feature, feature), axis=0)\n",
    "        label = self.label[index]\n",
    "        feature = feature.astype(\"float32\")\n",
    "        label = np.array([label], dtype=\"int64\")\n",
    "        return feature, label\n",
    "\n",
    "    def __len__(self):\n",
    "        \"\"\"\n",
    "        实现__len__方法，返回数据集总数目\n",
    "        \"\"\"\n",
    "        return len(self.feature)\n",
    "\n",
    "    def awgn(self, data, snr, seed=seed):\n",
    "        \"\"\"\n",
    "        添加高斯白噪声\n",
    "        \"\"\"\n",
    "        np.random.seed(seed)\n",
    "        snr = 10 ** (snr / 10.0)\n",
    "        xpower = np.sum(data**2) / len(data)\n",
    "        npower = xpower / snr\n",
    "        noise = np.random.randn(len(data)) * np.sqrt(npower)\n",
    "        return np.array(data + noise)\n",
    "\n",
    "    def standardization(self, train_data, val_data):\n",
    "        \"\"\"\n",
    "        标准化\n",
    "        \"\"\"\n",
    "        scalar = preprocessing.StandardScaler().fit(train_data)\n",
    "        train_data = scalar.transform(train_data)\n",
    "        val_data = scalar.transform(val_data)\n",
    "        return train_data, val_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f817908d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# MLP网络定义\n",
    "class MLPNet(nn.Layer):\n",
    "    def __init__(self, num_classes):\n",
    "        super(MLPNet, self).__init__()\n",
    "        self.layers = nn.Sequential(\n",
    "            nn.Linear(1024, 512), nn.BatchNorm1D(512), nn.ReLU(),\n",
    "            nn.Linear(512, 256), nn.BatchNorm1D(256), nn.ReLU(),\n",
    "            nn.Linear(256, 128), nn.BatchNorm1D(128), nn.ReLU(),\n",
    "            nn.Linear(128, 64), nn.BatchNorm1D(64), nn.ReLU(),\n",
    "            nn.Dropout(0.5),\n",
    "            nn.Linear(64, num_classes)\n",
    "        )\n",
    "    \n",
    "    def forward(self, x):\n",
    "        return self.layers(x) if self.training else F.softmax(self.layers(x))\n",
    "\n",
    "# CNN网络定义\n",
    "class CNNNet(nn.Layer):\n",
    "    def __init__(self, num_classes):\n",
    "        super(CNNNet, self).__init__()\n",
    "        self.conv_layers = nn.Sequential(\n",
    "            nn.Conv1D(1, 32, 3, padding=1), nn.BatchNorm1D(32), nn.ReLU(), nn.MaxPool1D(2),\n",
    "            nn.Conv1D(32, 64, 3, padding=1), nn.BatchNorm1D(64), nn.ReLU(), nn.MaxPool1D(2),\n",
    "            nn.Conv1D(64, 64, 3, padding=1), nn.BatchNorm1D(64), nn.ReLU(), nn.MaxPool1D(2)\n",
    "        )\n",
    "        self.fc_layers = nn.Sequential(\n",
    "            nn.Linear(8192, 100), nn.ReLU(), nn.Dropout(0.5),\n",
    "            nn.Linear(100, num_classes)\n",
    "        )\n",
    "    \n",
    "    def forward(self, x):\n",
    "        x = self.conv_layers(x)\n",
    "        x = paddle.flatten(x, 1)\n",
    "        return self.fc_layers(x) if self.training else F.softmax(self.fc_layers(x))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "16a9d216",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ResNet网络定义\n",
    "from paddle.vision.models import resnet18\n",
    "class ResNet(nn.Layer):\n",
    "    def __init__(self, num_classes):\n",
    "        super(ResNet, self).__init__()\n",
    "        self.backbone = resnet18(pretrained=False)\n",
    "        self.fc = nn.Sequential(\n",
    "            nn.Linear(1000, 512), nn.ReLU(), nn.Dropout(0.1),\n",
    "            nn.Linear(512, num_classes)\n",
    "        )\n",
    "    \n",
    "    def forward(self, x):\n",
    "        x = self.backbone(x)\n",
    "        return self.fc(x) if self.training else F.softmax(self.fc(x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "967cc008",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 数据集初始化\n",
    "time_steps, window, noise, snr = 1024, 128, True, -10\n",
    "val_rate, test_rate, network = 0.3, 0.5, \"ResNet\"  # 可选 'MLPNet', 'CNNNet', 'ResNet'\n",
    "\n",
    "train_dataset = CWRUDataset(\n",
    "    \"C:\\\\Users\\\\Henan\\\\Downloads\\\\data\\\\\", time_steps, window, \"train\", val_rate, test_rate, noise, snr, network\n",
    ")\n",
    "val_dataset = CWRUDataset(\n",
    "    \"C:\\\\Users\\\\Henan\\\\Downloads\\\\data\\\\\", time_steps, window, \"val\", val_rate, test_rate, noise, snr, network\n",
    ")\n",
    "test_dataset = CWRUDataset(\n",
    "    \"C:\\\\Users\\\\Henan\\\\Downloads\\\\data\\\\\", time_steps, window, \"test\", val_rate, test_rate, noise, snr, network\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0cfc045b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 训练函数\n",
    "def train_model(lr=1e-3, batch_size=16, epoch=5, num_classes=10):\n",
    "    model = paddle.Model(eval(network)(num_classes))\n",
    "    optim = paddle.optimizer.Adam(\n",
    "        learning_rate=lr,\n",
    "        parameters=model.parameters(),\n",
    "        weight_decay=paddle.regularizer.L2Decay(coeff=1e-5),\n",
    "    )\n",
    "    model.prepare(optim, nn.CrossEntropyLoss(), Accuracy())\n",
    "    callbacks = paddle.callbacks.EarlyStopping(\n",
    "        monitor=\"acc\", mode=\"max\", patience=100, verbose=1, save_best_model=True\n",
    "    )\n",
    "    model.fit(\n",
    "        train_dataset,\n",
    "        val_dataset,\n",
    "        epochs=epoch,\n",
    "        batch_size=batch_size,\n",
    "        callbacks=[callbacks],\n",
    "        save_dir=f\"{network}_checkpoints\",\n",
    "        save_freq=20,\n",
    "    )\n",
    "\n",
    "\n",
    "# 评估函数\n",
    "def evaluate_model():\n",
    "    model = paddle.Model(eval(network)(10))\n",
    "    model.load(f\"{network}_checkpoints/best_model.pdparams\")\n",
    "    model.prepare(metrics=Accuracy())\n",
    "    result = model.evaluate(test_dataset, verbose=1)\n",
    "    print(f\"{network}_test_acc: {result['acc']:.4f}\")\n",
    "\n",
    "\n",
    "# 预测函数\n",
    "def predict_model():\n",
    "    model = paddle.Model(eval(network)(10))\n",
    "    model.load(f\"{network}_checkpoints/best_model.pdparams\")\n",
    "    result = model.predict(test_dataset, verbose=1)\n",
    "    pred_prob = result[0][0][0].tolist()\n",
    "    pred_label = pred_prob.index(max(pred_prob))\n",
    "    true_label = test_dataset.__getitem__(0)[1][0]\n",
    "    print(f\"True Label: {true_label}, Pred Label: {pred_label}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "44aa0394",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The loss value printed in the log is the current step, and the metric is the average value of previous steps.\n",
      "Epoch 1/5\n",
      "step  10/456 - loss: 1.8186 - acc: 0.2687 - 269ms/step\n",
      "step  20/456 - loss: 1.5355 - acc: 0.3531 - 262ms/step\n",
      "step  30/456 - loss: 1.5161 - acc: 0.3854 - 259ms/step\n",
      "step  40/456 - loss: 1.6171 - acc: 0.4297 - 265ms/step\n",
      "step  50/456 - loss: 2.0798 - acc: 0.4487 - 269ms/step\n"
     ]
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[31m---------------------------------------------------------------------------\u001b[39m",
      "\u001b[31mKeyboardInterrupt\u001b[39m                         Traceback (most recent call last)",
      "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[11]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[43mtrain_model\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n",
      "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[9]\u001b[39m\u001b[32m, line 13\u001b[39m, in \u001b[36mtrain_model\u001b[39m\u001b[34m(lr, batch_size, epoch, num_classes)\u001b[39m\n\u001b[32m      9\u001b[39m model.prepare(optim, nn.CrossEntropyLoss(), Accuracy())\n\u001b[32m     10\u001b[39m callbacks = paddle.callbacks.EarlyStopping(\n\u001b[32m     11\u001b[39m     monitor=\u001b[33m\"\u001b[39m\u001b[33macc\u001b[39m\u001b[33m\"\u001b[39m, mode=\u001b[33m\"\u001b[39m\u001b[33mmax\u001b[39m\u001b[33m\"\u001b[39m, patience=\u001b[32m100\u001b[39m, verbose=\u001b[32m1\u001b[39m, save_best_model=\u001b[38;5;28;01mTrue\u001b[39;00m\n\u001b[32m     12\u001b[39m )\n\u001b[32m---> \u001b[39m\u001b[32m13\u001b[39m \u001b[43mmodel\u001b[49m\u001b[43m.\u001b[49m\u001b[43mfit\u001b[49m\u001b[43m(\u001b[49m\n\u001b[32m     14\u001b[39m \u001b[43m    \u001b[49m\u001b[43mtrain_dataset\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m     15\u001b[39m \u001b[43m    \u001b[49m\u001b[43mval_dataset\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m     16\u001b[39m \u001b[43m    \u001b[49m\u001b[43mepochs\u001b[49m\u001b[43m=\u001b[49m\u001b[43mepoch\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m     17\u001b[39m \u001b[43m    \u001b[49m\u001b[43mbatch_size\u001b[49m\u001b[43m=\u001b[49m\u001b[43mbatch_size\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m     18\u001b[39m \u001b[43m    \u001b[49m\u001b[43mcallbacks\u001b[49m\u001b[43m=\u001b[49m\u001b[43m[\u001b[49m\u001b[43mcallbacks\u001b[49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m     19\u001b[39m \u001b[43m    \u001b[49m\u001b[43msave_dir\u001b[49m\u001b[43m=\u001b[49m\u001b[33;43mf\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[38;5;132;43;01m{\u001b[39;49;00m\u001b[43mnetwork\u001b[49m\u001b[38;5;132;43;01m}\u001b[39;49;00m\u001b[33;43m_checkpoints\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[32m     20\u001b[39m \u001b[43m    \u001b[49m\u001b[43msave_freq\u001b[49m\u001b[43m=\u001b[49m\u001b[32;43m20\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[32m     21\u001b[39m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n",
      "\u001b[36mFile \u001b[39m\u001b[32mg:\\TempTest\\TempDownload\\tk_cwru\\.venv\\Lib\\site-packages\\paddle\\hapi\\model.py:2430\u001b[39m, in \u001b[36mModel.fit\u001b[39m\u001b[34m(self, train_data, eval_data, batch_size, epochs, eval_freq, log_freq, save_dir, save_freq, verbose, drop_last, shuffle, num_workers, callbacks, accumulate_grad_batches, num_iters)\u001b[39m\n\u001b[32m   2428\u001b[39m \u001b[38;5;28;01mfor\u001b[39;00m epoch \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mrange\u001b[39m(epochs):\n\u001b[32m   2429\u001b[39m     cbks.on_epoch_begin(epoch)\n\u001b[32m-> \u001b[39m\u001b[32m2430\u001b[39m     logs = \u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43m_run_one_epoch\u001b[49m\u001b[43m(\u001b[49m\u001b[43mtrain_loader\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcbks\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[33;43m'\u001b[39;49m\u001b[33;43mtrain\u001b[39;49m\u001b[33;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[32m   2431\u001b[39m     cbks.on_epoch_end(epoch, logs)\n\u001b[32m   2433\u001b[39m     \u001b[38;5;28;01mif\u001b[39;00m do_eval \u001b[38;5;129;01mand\u001b[39;00m epoch % eval_freq == \u001b[32m0\u001b[39m:\n",
      "\u001b[36mFile \u001b[39m\u001b[32mg:\\TempTest\\TempDownload\\tk_cwru\\.venv\\Lib\\site-packages\\paddle\\hapi\\model.py:2812\u001b[39m, in \u001b[36mModel._run_one_epoch\u001b[39m\u001b[34m(self, data_loader, callbacks, mode, logs)\u001b[39m\n\u001b[32m   2806\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m mode == \u001b[33m'\u001b[39m\u001b[33mtrain\u001b[39m\u001b[33m'\u001b[39m:\n\u001b[32m   2807\u001b[39m     _inputs.append(\n\u001b[32m   2808\u001b[39m         (step + \u001b[32m1\u001b[39m) % \u001b[38;5;28mself\u001b[39m._accumulate == \u001b[32m0\u001b[39m\n\u001b[32m   2809\u001b[39m         \u001b[38;5;129;01mor\u001b[39;00m step + \u001b[32m1\u001b[39m == \u001b[38;5;28mlen\u001b[39m(data_loader)\n\u001b[32m   2810\u001b[39m     )\n\u001b[32m-> \u001b[39m\u001b[32m2812\u001b[39m outs = \u001b[38;5;28;43mgetattr\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mmode\u001b[49m\u001b[43m \u001b[49m\u001b[43m+\u001b[49m\u001b[43m \u001b[49m\u001b[33;43m'\u001b[39;49m\u001b[33;43m_batch\u001b[39;49m\u001b[33;43m'\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m(\u001b[49m\u001b[43m*\u001b[49m\u001b[43m_inputs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m   2814\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m._metrics \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;28mself\u001b[39m._loss:\n\u001b[32m   2815\u001b[39m     metrics = [[\u001b[38;5;28mfloat\u001b[39m(l) \u001b[38;5;28;01mfor\u001b[39;00m l \u001b[38;5;129;01min\u001b[39;00m outs[\u001b[32m0\u001b[39m]]]\n",
      "\u001b[36mFile \u001b[39m\u001b[32mg:\\TempTest\\TempDownload\\tk_cwru\\.venv\\Lib\\site-packages\\paddle\\hapi\\model.py:1681\u001b[39m, in \u001b[36mModel.train_batch\u001b[39m\u001b[34m(self, inputs, labels, update)\u001b[39m\n\u001b[32m   1625\u001b[39m \u001b[38;5;28;01mdef\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34mtrain_batch\u001b[39m(\n\u001b[32m   1626\u001b[39m     \u001b[38;5;28mself\u001b[39m,\n\u001b[32m   1627\u001b[39m     inputs: _InputBatch,\n\u001b[32m   1628\u001b[39m     labels: _InputBatch | \u001b[38;5;28;01mNone\u001b[39;00m = \u001b[38;5;28;01mNone\u001b[39;00m,\n\u001b[32m   1629\u001b[39m     update: \u001b[38;5;28mbool\u001b[39m = \u001b[38;5;28;01mTrue\u001b[39;00m,\n\u001b[32m   1630\u001b[39m ) -> \u001b[38;5;28mlist\u001b[39m[\u001b[38;5;28mfloat\u001b[39m] | \u001b[38;5;28mtuple\u001b[39m[\u001b[38;5;28mlist\u001b[39m[npt.NDArray[Any]], \u001b[38;5;28mlist\u001b[39m[\u001b[38;5;28mfloat\u001b[39m]]:\n\u001b[32m   1631\u001b[39m \u001b[38;5;250m    \u001b[39m\u001b[33;03m\"\"\"\u001b[39;00m\n\u001b[32m   1632\u001b[39m \n\u001b[32m   1633\u001b[39m \u001b[33;03m    Run one training step on one batch of data. And using `update` indicates\u001b[39;00m\n\u001b[32m   (...)\u001b[39m\u001b[32m   1679\u001b[39m \n\u001b[32m   1680\u001b[39m \u001b[33;03m    \"\"\"\u001b[39;00m\n\u001b[32m-> \u001b[39m\u001b[32m1681\u001b[39m     loss = \u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43m_adapter\u001b[49m\u001b[43m.\u001b[49m\u001b[43mtrain_batch\u001b[49m\u001b[43m(\u001b[49m\u001b[43minputs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mlabels\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mupdate\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m   1682\u001b[39m     \u001b[38;5;28;01mif\u001b[39;00m in_dynamic_mode() \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;28mself\u001b[39m._input_info \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[32m   1683\u001b[39m         \u001b[38;5;28mself\u001b[39m._update_inputs()\n",
      "\u001b[36mFile \u001b[39m\u001b[32mg:\\TempTest\\TempDownload\\tk_cwru\\.venv\\Lib\\site-packages\\paddle\\hapi\\model.py:1275\u001b[39m, in \u001b[36mDynamicGraphAdapter.train_batch\u001b[39m\u001b[34m(self, inputs, labels, update)\u001b[39m\n\u001b[32m   1273\u001b[39m         \u001b[38;5;28mself\u001b[39m.model.network.clear_gradients()\n\u001b[32m   1274\u001b[39m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[32m-> \u001b[39m\u001b[32m1275\u001b[39m     \u001b[43mfinal_loss\u001b[49m\u001b[43m.\u001b[49m\u001b[43mbackward\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m   1276\u001b[39m     \u001b[38;5;28;01mif\u001b[39;00m update:\n\u001b[32m   1277\u001b[39m         \u001b[38;5;28mself\u001b[39m.model._optimizer.minimize(final_loss)\n",
      "\u001b[36mFile \u001b[39m\u001b[32mg:\\TempTest\\TempDownload\\tk_cwru\\.venv\\Lib\\site-packages\\decorator.py:235\u001b[39m, in \u001b[36mdecorate.<locals>.fun\u001b[39m\u001b[34m(*args, **kw)\u001b[39m\n\u001b[32m    233\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m kwsyntax:\n\u001b[32m    234\u001b[39m     args, kw = fix(args, kw, sig)\n\u001b[32m--> \u001b[39m\u001b[32m235\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mcaller\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfunc\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m*\u001b[49m\u001b[43m(\u001b[49m\u001b[43mextras\u001b[49m\u001b[43m \u001b[49m\u001b[43m+\u001b[49m\u001b[43m \u001b[49m\u001b[43margs\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m*\u001b[49m\u001b[43m*\u001b[49m\u001b[43mkw\u001b[49m\u001b[43m)\u001b[49m\n",
      "\u001b[36mFile \u001b[39m\u001b[32mg:\\TempTest\\TempDownload\\tk_cwru\\.venv\\Lib\\site-packages\\paddle\\base\\wrapped_decorator.py:40\u001b[39m, in \u001b[36mwrap_decorator.<locals>.__impl__\u001b[39m\u001b[34m(func, *args, **kwargs)\u001b[39m\n\u001b[32m     33\u001b[39m \u001b[38;5;129m@decorator\u001b[39m.decorator\n\u001b[32m     34\u001b[39m \u001b[38;5;28;01mdef\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34m__impl__\u001b[39m(\n\u001b[32m     35\u001b[39m     func: Callable[_InputT, _RetT1],\n\u001b[32m     36\u001b[39m     *args: _InputT.args,\n\u001b[32m     37\u001b[39m     **kwargs: _InputT.kwargs,\n\u001b[32m     38\u001b[39m ) -> _RetT2:\n\u001b[32m     39\u001b[39m     wrapped_func = decorator_func(func)\n\u001b[32m---> \u001b[39m\u001b[32m40\u001b[39m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mwrapped_func\u001b[49m\u001b[43m(\u001b[49m\u001b[43m*\u001b[49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m*\u001b[49m\u001b[43m*\u001b[49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n",
      "\u001b[36mFile \u001b[39m\u001b[32mg:\\TempTest\\TempDownload\\tk_cwru\\.venv\\Lib\\site-packages\\paddle\\base\\framework.py:704\u001b[39m, in \u001b[36m_dygraph_only_.<locals>.__impl__\u001b[39m\u001b[34m(*args, **kwargs)\u001b[39m\n\u001b[32m    700\u001b[39m \u001b[38;5;28;01mdef\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34m__impl__\u001b[39m(*args: _InputT.args, **kwargs: _InputT.kwargs) -> _RetT:\n\u001b[32m    701\u001b[39m     \u001b[38;5;28;01massert\u001b[39;00m (\n\u001b[32m    702\u001b[39m         in_dygraph_mode()\n\u001b[32m    703\u001b[39m     ), \u001b[33mf\u001b[39m\u001b[33m\"\u001b[39m\u001b[33mWe only support \u001b[39m\u001b[33m'\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mfunc.\u001b[34m__name__\u001b[39m\u001b[38;5;132;01m}\u001b[39;00m\u001b[33m()\u001b[39m\u001b[33m'\u001b[39m\u001b[33m in dynamic graph mode, please call \u001b[39m\u001b[33m'\u001b[39m\u001b[33mpaddle.disable_static()\u001b[39m\u001b[33m'\u001b[39m\u001b[33m to enter dynamic graph mode.\u001b[39m\u001b[33m\"\u001b[39m\n\u001b[32m--> \u001b[39m\u001b[32m704\u001b[39m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mfunc\u001b[49m\u001b[43m(\u001b[49m\u001b[43m*\u001b[49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m*\u001b[49m\u001b[43m*\u001b[49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n",
      "\u001b[36mFile \u001b[39m\u001b[32mg:\\TempTest\\TempDownload\\tk_cwru\\.venv\\Lib\\site-packages\\paddle\\base\\dygraph\\tensor_patch_methods.py:357\u001b[39m, in \u001b[36mmonkey_patch_tensor.<locals>.backward\u001b[39m\u001b[34m(self, grad_tensor, retain_graph)\u001b[39m\n\u001b[32m    353\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m _grad_scalar:\n\u001b[32m    354\u001b[39m     \u001b[38;5;66;03m# When using amp with Fleet DistributedStrategy, we do loss scaling implicitly.\u001b[39;00m\n\u001b[32m    355\u001b[39m     \u001b[38;5;28mself\u001b[39m = _grad_scalar.scale(\u001b[38;5;28mself\u001b[39m)\n\u001b[32m--> \u001b[39m\u001b[32m357\u001b[39m \u001b[43mcore\u001b[49m\u001b[43m.\u001b[49m\u001b[43meager\u001b[49m\u001b[43m.\u001b[49m\u001b[43mrun_backward\u001b[49m\u001b[43m(\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mgrad_tensor\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mretain_graph\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m    359\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m in_profiler_mode():\n\u001b[32m    360\u001b[39m     record_event.end()\n",
      "\u001b[31mKeyboardInterrupt\u001b[39m: "
     ]
    }
   ],
   "source": [
    "train_model()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "570cb1fa",
   "metadata": {},
   "outputs": [],
   "source": [
    "evaluate_model()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1e07d41f",
   "metadata": {},
   "outputs": [],
   "source": [
    "predict_model()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
